package com.ousi.wx.controller;

import com.ousi.wx.common.CommonResult;
import com.ousi.wx.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/product")
@Slf4j
@Tag(name = "ProductController", description = "产品信息接口")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/selectProductNameAndId")
    @Operation(summary = "查询所有产品名字和ID")
//    @CrossOrigin(origins = "localhost:5173", allowCredentials = "true")
    public CommonResult selectProductNameAndId() {
        try {
            ArrayList<HashMap> list = productService.selectProductNameAndId();
            return CommonResult.ok().put(CommonResult.RETURN_RESULT, list);
        } catch (Exception e) {
            log.error("查询过程中出现错误", e);
            return CommonResult.ok().put("查询过程中出现错误", e);
        }
    }

    @PostMapping("/selectProductById")
    @Operation(summary = "根据产品Id查询相关信息")
    public CommonResult selectProductById(@RequestParam("id") Integer id) {
        try {
            HashMap map = productService.selectProductById(id);
            return CommonResult.ok().put(CommonResult.RETURN_RESULT, map);
        } catch (Exception e) {
            log.error("查询过程中出现错误", e);
            return CommonResult.ok().put("查询过程中出现错误", e);
        }
    }

    @PostMapping("/getProductImages")
    @Operation(summary = "查询产品图片")
    public CommonResult getProductImages(@RequestParam("productId") Integer productId){
        List<String>  list = productService.selectPhotoById(productId);
        return CommonResult.ok().put("result", list);
    }

}
